package com.mashibing.cor.servlet;

import java.util.ArrayList;
import java.util.List;

public class ServletMain {
    public static void main(String[] args) {
        //Msg msg = new Msg();
        //msg.setMsg("大家好:)，<script>,欢迎访问mashibing.com,大家都是996");

        //处理msg
        //String r = msg.getMsg();
        //r = r.replace('<','[');
        //r = r.replace('>',']');
        //msg.setMsg(r);
        //
        //r = r.replaceAll("996", "995");
        //msg.setMsg(r);

        //FilterChain fc = new FilterChain();
        //fc.add(new HTMLFilter())
        //        .add(new SensitiveFilter());
        //
        //FilterChain fc2 = new FilterChain();
        //fc2.add(new FaceFilter())
        //        .add(new UrlFilter());
        //
        //fc.add(fc2);
        //fc.doFilter(msg);

        Request request = new Request();
        request.str = "大家好:)，<script>,欢迎访问mashibing.com,大家都是996";
        Response response = new Response();
        response.str = "response";

        FilterChain chain = new FilterChain();
        chain.add(new HTMLFilter()).add(new SensitiveFilter());
        chain.doFilter(request, response, chain);
        System.out.println(request.str);
        System.out.println(response.str);


        //System.out.println(msg);
    }
}

class Msg {
    String name;
    String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "Msg{" +
                "msg='" + msg + '\'' +
                '}';
    }
}

interface Filter {
    Boolean doFilter(Request request, Response response, FilterChain chain);
}

class HTMLFilter implements Filter {
    @Override
    public Boolean doFilter(Request request, Response response, FilterChain chain) {
        //String r = m.getMsg();
        //r = r.replace('<', '[');
        //r = r.replace('>', ']');
        //m.setMsg(r);
        //return true;
        request.str = request.str.replaceAll("<", "[").replaceAll(">", "]") + " HTMLFilter";
        chain.doFilter(request, response, chain);
        response.str = response.str + "--HTMLFilter";
        return false;
    }
}

class Request {
    String str;
}

class Response {
    String str;
}

class SensitiveFilter implements Filter {
    @Override
    public Boolean doFilter(Request request, Response response, FilterChain chain) {
        request.str = request.str.replaceAll("996", "995") + " SensitiveFilter";
        chain.doFilter(request, response, chain);
        response.str = response.str + "--SensitiveFilter";
        return false;
    }
}

//class FaceFilter implements Filter {
//    @Override
//    public Boolean doFilter(Msg m) {
//        String r = m.getMsg();
//        r = r.replace(":)", "^V^");
//        m.setMsg(r);
//        return true;
//    }
//}
//
//class UrlFilter implements Filter {
//    @Override
//    public Boolean doFilter(Msg m) {
//        String r = m.getMsg();
//        r = r.replace("mashibing.com", "http://mashibing.com");
//        m.setMsg(r);
//        return true;
//    }
//}

class FilterChain implements Filter {
    List<Filter> filters = new ArrayList<>();
    int index = 0;

    public FilterChain add(Filter f) {
        filters.add(f);
        return this;
    }

    public Boolean doFilter(Request request, Response response, FilterChain chain) {
        if (index == filters.size()) return false;
        Filter f = filters.get(index);
        index++;
        f.doFilter(request, response, chain);
        return false;
    }
}
